package com.ydgk.controller;

import com.ydgk.bean.KindsType;
import com.ydgk.service.KindsTypeService;
import com.ydgk.tool.Tool;
import com.ydgk.vo.AjaxResult;
import com.ydgk.vo.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

@RequestMapping("/kindsType")
@Controller
public class KindsTypeController {

    private static final String UPLOAD_DIRECTORY = "upload/kindsTypeFace";
    private static MultipartFile fileForInsert = null;
    private static MultipartFile fileForUpdate = null;

    @Autowired
    private KindsTypeService kindsTypeService;

    @ModelAttribute
    public void getKinds(@RequestParam(value = "ktid" , required = false , defaultValue = "0") Integer ktid, Map map){
        if(ktid != 0){
            KindsType kindsType = kindsTypeService.findById(ktid);
            map.put("kindsType",kindsType);
        }
    }

    @ResponseBody
    @RequestMapping("/ktfaceUpload")
    public AjaxResult kfaceUpload(@RequestParam("file") MultipartFile file) throws IOException {
        this.fileForInsert = file;
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(true);
        return ajaxResult;
    }

    @ResponseBody
    @RequestMapping("/ktfaceUpdate")
    public AjaxResult kfaceUpdate(@RequestParam("file") MultipartFile file) throws IOException {
        this.fileForUpdate = file;
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(true);
        return ajaxResult;
    }

    @ResponseBody
    @RequestMapping("/validationExist")
    public AjaxResult validationExist(KindsType kindsType, String newKtname, Integer kid) {
        AjaxResult ajaxResult = new AjaxResult();
        List<KindsType> kindsTypeList = kindsTypeService.findAllByKid(kid);
        if(kindsType.getKtid() != null){
            kindsTypeList.remove(kindsType);
        }
        for (KindsType kt: kindsTypeList) {
            if(kt.getKtname().equals(newKtname)){
                ajaxResult.setSuccess(true);
                return ajaxResult;
            }
        }
        ajaxResult.setSuccess(false);
        return ajaxResult;

    }

    @ResponseBody
    @RequestMapping(value = "/insertKindsType", method = RequestMethod.POST)
    public AjaxResult insertKinds(@RequestParam("flag") Boolean flag, HttpServletRequest request, KindsType kindsType) throws IOException {
        if (flag) {
            String newfilename = Tool.upload(request,fileForInsert,UPLOAD_DIRECTORY);
            kindsType.setKtface(UPLOAD_DIRECTORY + "/" + newfilename);
        }else{
            kindsType.setKtface("img/logo.png");
        }
        int i = kindsTypeService.insert(kindsType);
        AjaxResult ajaxResult = new AjaxResult();
        if (i > 0) {
            ajaxResult.setSuccess(true);
        } else {
            ajaxResult.setSuccess(false);
        }
        return ajaxResult;
    }

    @ResponseBody
    @RequestMapping(value = "/findAllByKid", method = RequestMethod.POST)
    public AjaxResult findAllByKid(Integer kid) {
        AjaxResult ajaxResult = new AjaxResult();
        try {
            List<KindsType> kindsTypeList = kindsTypeService.findAllByKid(kid);
            Page<KindsType> page = new Page(kindsTypeList, null, null,null,null);
            ajaxResult.setData(page);
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            ajaxResult.setSuccess(false);
            e.printStackTrace();
        }
 /*   try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/
        return ajaxResult;
    }

    @ResponseBody
    @RequestMapping(value = "/deleteKindsType/{ktid}", method = RequestMethod.DELETE)
    public AjaxResult deleteKinds(@PathVariable("ktid") Integer ktid, HttpServletRequest request, KindsType kindsType) {
        ServletContext application = request.getServletContext();
        AjaxResult ajaxResult = new AjaxResult();
        try {
            int i = kindsTypeService.deleteById(ktid);
            if (i > 0) {
                String ktface = kindsType.getKtface();
                if(ktface != null && !("".equals(ktface)) && !("img/logo.png".equals(ktface))){
                    new File(application.getRealPath("/"+ktface)).delete();
                }
                ajaxResult.setSuccess(true);
            } else {
                ajaxResult.setSuccess(false);
            }
        } catch (Exception e) {
            ajaxResult.setSuccess(false);
            e.printStackTrace();
        }
        return ajaxResult;
    }

    @ResponseBody
    @RequestMapping(value = "/updateKindsType/{ktid}", method = RequestMethod.PUT)
    public AjaxResult updateKinds(@RequestParam("flag") Boolean flag, HttpServletRequest request, KindsType kindsType) throws IOException {
        if (flag) {
            ServletContext application = request.getServletContext();
            String newfilename = Tool.upload(request,fileForUpdate,UPLOAD_DIRECTORY);
            String ktface = kindsType.getKtface();
            if(ktface != null && !("".equals(ktface)) && !("img/logo.png".equals(ktface))){
                new File(application.getRealPath("/"+ktface)).delete();
            }
            kindsType.setKtface(UPLOAD_DIRECTORY + "/" + newfilename);
        }
        int i = kindsTypeService.updateById(kindsType);
        AjaxResult ajaxResult = new AjaxResult();
        if (i > 0) {
            ajaxResult.setSuccess(true);
        } else {
            ajaxResult.setSuccess(false);
        }
        return ajaxResult;
    }


}
